package com.api.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.api.common.reqres.Request;
import com.api.common.reqres.RequestHead;
import com.api.common.reqres.Response;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.BackstageUserMapper;
import com.api.mapper.RoleMapper;
import com.api.mapper.RoleMenuMapper;
import com.api.model.entity.Role;
import com.api.model.entity.RoleMenu;
import com.api.model.request.AddRoleRequest;
import com.api.model.request.RoleAddMenuRequest;
import com.api.model.request.RoleListRequest;
import com.api.model.request.UpdateRoleRequest;
import com.api.model.response.NullResponse;
import com.api.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;
	@Autowired
	private BackstageUserMapper backstageUserMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;

	@Override
	public Response<NullResponse> roleList(Request<RoleListRequest> request) {
		// TODO Auto-generated method stub
		RoleListRequest roleListRequest = request.getBody();
		requestComponent.checkBody(roleListRequest, RoleListRequest.class);
		LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<Role>();
		queryWrapper.eq(Role::getStatus, true);
		List<Role> roleList = roleMapper.selectList(queryWrapper);
		return responseComponent.toSuccess(roleList);
	}

	@Override
	public Response<NullResponse> updateRole(Request<UpdateRoleRequest> request) {
		// TODO Auto-generated method stub
		RequestHead head = request.getHead();
		UpdateRoleRequest updateRoleRequest = request.getBody();
		requestComponent.checkBody(updateRoleRequest, UpdateRoleRequest.class);
		Role role = new Role();
		role.setUpdatetime(LocalDateTime.now());
		role.setUpdateuser(head.getUserid());
		BeanUtils.copyProperties(updateRoleRequest, role);
		roleMapper.updateById(role);
		return responseComponent.toSuccess(null);
	}

	@Override
	public Response<NullResponse> addRole(Request<AddRoleRequest> request) {
		// TODO Auto-generated method stub
		RequestHead head = request.getHead();
		AddRoleRequest addRoleRequest = request.getBody();
		requestComponent.checkBody(addRoleRequest, AddRoleRequest.class);
		Role role = new Role();
		role.setCreateuser(head.getUserid());
		role.setCreatetime(LocalDateTime.now());
		BeanUtils.copyProperties(addRoleRequest, role);
		roleMapper.insert(role);
		return responseComponent.toSuccess(null);
	}

	@Override
	public Response<NullResponse> roleAddMenu(Request<RoleAddMenuRequest> request) {
		// TODO Auto-generated method stub
		RequestHead head = request.getHead();
		RoleAddMenuRequest roleAddMenuRequest = request.getBody();
		requestComponent.checkBody(roleAddMenuRequest, RoleAddMenuRequest.class);
		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setCreateuser(head.getUserid());
		roleMenu.setCreatetime(LocalDateTime.now());
		BeanUtils.copyProperties(roleAddMenuRequest, roleMenu);
		roleMenuMapper.insert(roleMenu);
		return responseComponent.toSuccess(null);
	}

}
